package com.yx.frame.service;

import com.yx.frame.jdbc.Sql;
import com.yx.frame.log.LogFactory;
import com.yx.frame.log.LogWriter;
import com.yx.frame.server.Application;
import com.yx.frame.authority.EncryptTools;
import com.yx.frame.authority.User;


public abstract class Service {

	static LogWriter log = LogFactory.getInstance().getLogWriter();

	public final static Sql getSql() throws Exception {
		return new Sql();
	}

	public final static Sql getSql(String jndi) throws Exception {
		return new Sql(jndi);
	}

	public final static void setServiceMemStorage(String key, ServiceResult obj,
			int timeOut) throws Exception {
		try {
			String md5Key = EncryptTools.MD5(key);
			Application.getProvider().setCache(md5Key, timeOut,obj.toString());			
		} catch (Exception e) {
		}
	}

	public final static ServiceResult getServiceMemStorage(String key) throws Exception {
		try {
			String md5Key = EncryptTools.MD5(key);			
			String mamcacheJsonString = (String) Application.getProvider().getCached(md5Key);
			if (null == mamcacheJsonString || "".equals(mamcacheJsonString)) {
				return null;
			}
			return new ServiceResult(mamcacheJsonString);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}
	public final static void debug(User user, String method, String msg) {
		log.write(LogWriter.lOG_DEBUG, user, method, msg);
	}

	public final static void info(User user, String method, String msg) {
		log.write(LogWriter.lOG_INFO, user, method, msg);
	}

	public final static void warn(User user, String method, String msg) {
		log.write(LogWriter.lOG_WARN, user, method, msg);
	}

	public final static void error(User user, String method, String msg) {
		log.write(LogWriter.lOG_ERROR, user, method, msg);
	}

	public final static void fatal(User user, String method, String msg) {
		log.write(LogWriter.lOG_FATAL, user, method, msg);
	}

}
